package com.dyz.leetcode._17_comprehensiveApplication;

import java.util.HashSet;
import java.util.Set;

public class _202_isHappy {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (true){
            if(n==1){return true;}
            if(set.contains(n)){return false;}
            set.add(n);
            n = squareSum(n);
        }

    }

    private int  squareSum(int n){
        int sum = 0;
        while (n!=0){
            int num = n %10;
            sum += num * num;
            n  = n/10;
        }
        return sum;
    }
}
